package com.yanqu.road.dao.impl.activity;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.rank.stronghold.CrossActivityUnionRank;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossGeneralActivityUnionRankDaoImpl extends TempDao {

    public Map<Integer, Map<Integer, LinkedList<CrossActivityUnionRank>>> getCrossActivityUnionRankListMap(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, LinkedList<CrossActivityUnionRank>>> dataMap = new ConcurrentHashMap<>();
        if (null == activityIdList || activityIdList.size() <= 0) {
            return dataMap;
        }
        String sql = "select * from t_c_activity_general_union_rank where activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossActivityUnionRank data = initUnionActivityRank(rs);
                if (!dataMap.containsKey(data.getActivityId())) {
                    dataMap.put(data.getActivityId(), new ConcurrentHashMap<>(2));
                }
                Map<Integer, LinkedList<CrossActivityUnionRank>> listMap = dataMap.get(data.getActivityId());
                LinkedList<CrossActivityUnionRank> rankList = listMap.get(data.getConditionType());
                if (rankList == null) {
                    rankList = new LinkedList<>();
                }
                rankList.add(data);
                listMap.put(data.getConditionType(), rankList);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean updateCrossActivityUnionRankList(int activityId, int conditionType, List<CrossActivityUnionRank> rankList) {
        String sql = "REPLACE INTO t_c_activity_general_union_rank(activityId,conditionType,rank,serverId,unionUid,unionBaseInfo,lastUpdateTime,rankValue,isDel) VALUES(?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            int rank = 1;
            for (CrossActivityUnionRank unionRank : rankList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER, activityId));
                para.put(i++, new DbParameter(Types.INTEGER, conditionType));
                para.put(i++, new DbParameter(Types.INTEGER, rank));
                para.put(i++, new DbParameter(Types.BIGINT, unionRank.getServerId()));
                para.put(i++, new DbParameter(Types.VARCHAR, unionRank.getUnionUid()));
                para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(unionRank.getUnionBaseInfo())));
                para.put(i++, new DbParameter(Types.BIGINT, unionRank.getLastUpdateTime()));
                para.put(i++, new DbParameter(Types.VARCHAR, unionRank.getValue().toString()));
                para.put(i++, new DbParameter(Types.BOOLEAN, unionRank.isDel()));
                mapList.add(para);
                rank++;
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    private CrossActivityUnionRank initUnionActivityRank(ResultSet rs) throws SQLException {
        CrossActivityUnionRank data = new CrossActivityUnionRank();
        data.setActivityId(rs.getInt("activityId"));
        data.setConditionType(rs.getInt("conditionType"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        String value = rs.getString("rankValue");
        if (!StringUtils.isNullOrEmpty(value)) {
            data.setValue(new BigInteger(value));
        }
        value = rs.getString("unionBaseInfo");
        if (null == value) {
            value = "";
        }
        UnionBaseInfo unionBaseInfo = JSON.parseObject(value, UnionBaseInfo.class);
        if (null != unionBaseInfo) {
            data.setUnionBaseInfo(unionBaseInfo);
        }
        data.setDel(rs.getBoolean("isDel"));
        data.setServerId(rs.getLong("serverId"));
        return data;
    }
}
